//
//  FirstScreen.m
//  FleaFinder
//
//  Created by Hieu Le on 4/8/13.
//  Copyright (c) 2013 Kiwiweb. All rights reserved.
//

#import "FirstScreen.h"
#import "FleaFinderNavigator.h"
#import "MarkedTypeViewController.h"
#import "XmlParserMarkedTypeList.h"
#import "MarkedTypeCollectionViewCell.h"
#import <QuartzCore/QuartzCore.h>
#import "MarkedSummaryListViewController.h"


@interface FirstScreen ()
    
@end

@implementation FirstScreen

@synthesize btnVisAlle;
@synthesize markedTypeListView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    font = [UIFont fontWithName:@"Helvetica" size:14.0];
    commonColor = [UIColor colorWithRed:0.184 green:0.301 blue:0.517 alpha:1.0];
    
    [self setTitle:@"Tilbage"];
    [[self.tabBarController.tabBar.items objectAtIndex:0] setTitle:@"Forside"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
    if(!appDelegate){
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    }
    
    [self loadXmlData:appDelegate.common.linkToMarkedTypeList];
    [self.markedTypeListView reloadData];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void) loadXmlData: (NSString*) sourceUrl
{
    if(!appDelegate){
        appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    }
    if([appDelegate.marketTypeList count] == 0){
        //Load all MarketTypes from website
    
        XmlParserMarkedTypeList *xmlParser = [[XmlParserMarkedTypeList alloc] init];
        [xmlParser parseXML: sourceUrl];
        
        appDelegate.marketTypeList = [xmlParser getParsedObjects];
    }
}

-(void) showCategoryListForMarket: (MarketType*) markedType
{
    if(!appDelegate.markedTypeViewController){
    
        if(!appDelegate.storyBoard)
            appDelegate.storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        
        appDelegate.markedTypeViewController = [appDelegate.storyBoard instantiateViewControllerWithIdentifier:@"SBMarkedType"];
    }
    [appDelegate.markedTypeViewController loadXmlDataForMarketType: markedType fromUrl: appDelegate.common.linkToCategoryList];// reload:TRUE];
    
    [self.navigationController pushViewController:appDelegate.markedTypeViewController animated:YES];
}


-(IBAction)showAllMarked:(id)sender
{
    // NSString *linkViewAll;
    
    appDelegate.currentSelectedMarketType = nil;
    
    if(!appDelegate.marketSummaryListViewController){
        
        if(!appDelegate.storyBoard)
            appDelegate.storyBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        
        appDelegate.marketSummaryListViewController = [appDelegate.storyBoard instantiateViewControllerWithIdentifier:@"SBMarkedSummaryList"];
    }
    appDelegate.marketSummaryListViewController.isNotTopMarkderMode = YES;
    [appDelegate.marketSummaryListViewController loadXmlData:appDelegate.common.linkViewAll isViewAll:YES];
    [self.navigationController pushViewController:appDelegate.marketSummaryListViewController animated:YES];
}

- (void)viewDidUnload {
    [self setBtnVisAlle:nil];
    [super viewDidUnload];
}

#pragma UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [appDelegate.marketTypeList count];
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MarkedTypeCollectionViewCell";

    MarkedTypeCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    cell.btnMarkedType.titleLabel.font = font;
    cell.btnMarkedType.titleLabel.textColor = commonColor;

    cell.btnMarkedType.titleLabel.textAlignment = UITextAlignmentCenter;
    cell.btnMarkedType.titleLabel.text = ((MarketType*)[appDelegate.marketTypeList objectAtIndex:indexPath.row]).name;
    
    [cell.btnMarkedType addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];

    return cell;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


-(void)buttonAction:(UIButton*)button {
    UICollectionViewCell *cell = (UICollectionViewCell*)button.superview.superview;
    NSIndexPath *path = [self.markedTypeListView indexPathForCell:cell];
    
    appDelegate.currentSelectedMarketType = ((MarketType*)[appDelegate.marketTypeList objectAtIndex:path.row]);
    
    [self showCategoryListForMarket:appDelegate.currentSelectedMarketType];
}

@end
